#include "head.h"
int makeWorker(int workernum, workerdata_t * workerdataArr){
    // 创建子进程 保存每个子进程的信息
    pid_t pid;
    int sv[2];
    for(int i = 0; i < workernum; ++i){
        socketpair(AF_UNIX,SOCK_STREAM,0,sv);
        pid = fork();
        if(pid == 0){
            // 子进程 让代码无法脱离if结构
            close(sv[1]);
            handle(sv[0]);
        }
        close(sv[0]);
        workerdataArr[i].pid = pid;
        workerdataArr[i].status = FREE;
        workerdataArr[i].sockfd = sv[1];
        printf("worker %d, pid = %d\n",i,pid);
    }
    return 0;
}
int handle(int sockfd){
    while(1){
        // 接收任务
        int netfd;
        recvfd(sockfd,&netfd);
        // 执行任务
        transfile(netfd);
        close(netfd);
        pid_t pid = getpid();
        send(sockfd,&pid,sizeof(pid),0);
    }
}
